/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     摄像头打开与关闭
 @FileName :    widget.cpp
 @Version :     1.0
****************************************************/


#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    
    ui->exit->raise();
    PushButton_Tcp(ui->exit);
    
    ui->label_bj->setPixmap(QPixmap(":/主题背景"));
    
    ui->label_zt->setText("智能猫眼");
    ui->label_zt->setStyleSheet("QLabel {"
        "    font-size: 50px;"           // 字体大小
        "    color: #1837ae;"            // 文字颜色
        "    font-weight: bold;"         // 字体粗细
        "    font-family: '楷体';"        // 字体家族
        "}"
    );
        
    ui->pushButton_kq->setText("");
    ui->pushButton_kq->setFlat(true);
    ui->pushButton_kq->setIcon(QIcon(":/摄像头_关"));     
    ui->pushButton_kq->setIconSize(QSize(100, 70));

    QTimer *mytimer = new QTimer(this);
    connect(mytimer,&QTimer::timeout,this, &Widget::myslot);
    mytimer->start();
    
    flag = 0;
    
    // 连接信号与槽
    connect(ui->pushButton_kq, &QPushButton::clicked, this, &Widget::Camera_state);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::Camera_state()
{
    State = !State;  // 切换状态
    
    if (State) {
        flag = 1;
        connect(&vapi, &V4l2Api::sendImage, this, &Widget::recvImage);
        vapi.start();
        
        ui->pushButton_kq->setIcon(QIcon(":/摄像头_开"));
    } else {
        flag = 0;
        disconnect(&vapi, &V4l2Api::sendImage, this, &Widget::recvImage);
        ui->label_Camera->clear();
        
        ui->pushButton_kq->setIcon(QIcon(":/摄像头_关"));
    }
}

//显示图像
void Widget::recvImage(QImage image)
{
    Saveimage = image;
    QPixmap mmp = QPixmap::fromImage(image);
    ui->label_Camera->setPixmap(mmp);
    ui->label_Camera->setScaledContents(true);
}

//显示清理，做刷新
void Widget::myslot()
{
    if(flag == 0)
    {
        ui->label_Camera->clear();
    }
}

void Widget::PushButton_Tcp(QPushButton* PushButton)
{
    PushButton->setStyleSheet(R"(
                              QPushButton {
                              background-color: #4285f4;
                              color: white;
                              border-radius: 15px;
                              border: none;
                              padding: 4px 4px;
                              font-size: 20px;
                              font-weight: bold;
                              font-family: "楷体";
                              }
                              QPushButton:hover {
                              background-color: #3367d6;
                              }
                              QPushButton:pressed {
                              background-color: #2a56c6;
                              }
                              )");      // padding: 4px 4px; 内边距 - 上下4px，左右4px
}

void Widget::on_exit_clicked()
{
    flag = 0;
    disconnect(&vapi, &V4l2Api::sendImage, this, &Widget::recvImage);
    ui->label_Camera->clear();
    
    this->close();
    QApplication::quit();
}


